package xyj.sparrow.scheduler.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import xyj.sparrow.scheduler.common.Result;
import xyj.sparrow.scheduler.entity.Client;
import xyj.sparrow.scheduler.service.IClientService;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jim
 * @since 2021-06-05
 */
@RestController
@RequestMapping("/api/v1/client")
@Slf4j
@CrossOrigin
public class ClientController {
    @Resource
    private IClientService iClientService;
    @GetMapping("")
    public Result<List<Client>> listApp() {
        List<Client> clients = iClientService.list();
        return Result.success(clients);
    }

    @PostMapping("")
    public Result<Client> addClient(@RequestBody Client client) {
        log.info("addApp:{}", client);
        client.setCreateTime(LocalDateTime.now());
        client.setUpdateTime(LocalDateTime.now());
        iClientService.save(client);
        return Result.success(client);
    }

    @PutMapping("")
    public Result<Object> updateClient(@RequestBody Client client) {
        log.info("updateApp:{}", client);
        client.setUpdateTime(LocalDateTime.now());
        iClientService.updateById(client);
        return Result.success();
    }

    @DeleteMapping("/{clientId}")
    public Result<Object> deleteClient(@PathVariable("clientId") String clientId) {
        log.info("deleteClient:{}", clientId);
        iClientService.removeById(clientId);
        return Result.success();
    }
}

